﻿using System;
using System.Reflection;
using System.Web.Mvc;

namespace Sencha.Direct
{
	[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
	public class DirectOnlyAttribute : ActionMethodSelectorAttribute
	{
		public static bool IsDefined(ICustomAttributeProvider attrs)
		{
			if (attrs == null)
				throw new ArgumentNullException("attrs");

			var foundAttrs = attrs.GetCustomAttributes(typeof(NonDirectActionAttribute), true);
			return (foundAttrs != null && foundAttrs.Length > 0);
		}

		public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
		{
			// forbids routed requests
			return IsDefined(methodInfo) && controllerContext.RequestContext.IsDirectRouted();
		}
	}
}
